<template>
  <div>
    <div style="margin-bottom: 15px;">
      <!-- 顶部添加数据 -->
      <span>员工姓名：</span>
      <el-input v-model="params.ename" style="width: 150px; margin-right: 15px;" placeholder="请输入员工姓名"></el-input>
      <el-button type="warning" style="margin-left: 10px;" @click='searchUser()'>查询</el-button>
      <el-button type="warning" style="margin-left: 10px;" @click='reset()'>清空</el-button>
      <el-button type="primary" style="margin-left: 10px;" @click='insert()'>新增</el-button>
    </div>

    <div>
      <!-- 引入表格 -->
      <el-table
        :data="tableData"
        :border="true"
        max-height="600">
        style="width: 100%">
        <!-- 表格内容 -->
        <el-table-column fixed prop="eno" label="员工编号" width="100"></el-table-column>
        <el-table-column prop="ename" label="员工姓名" width="150"></el-table-column>
        <el-table-column prop="account" label="账号" width="200"></el-table-column>

        <el-table-column  label="操作" width="200">
          <template slot-scope="scope">
            <el-button type="primary" @click="edit(scope.row)">编辑</el-button>
            <el-popconfirm title="确定删除吗？" @confirm="del(scope.row.eno)">
                <el-button slot="reference" type="danger" style="margin-left: 5px;">删除</el-button>
              </el-popconfirm>
          </template>
          
        </el-table-column>
      </el-table>
    </div>

    <!-- 分页显示 -->
      <div>
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="params.pageNum"
          :page-sizes="[5, 10, 15, 20]" 
          :page-size="params.pageSize"
          layout="total, sizes, prev, pager, next"
          :total="total">
        </el-pagination>
      </div>

      <!-- 对话框 -->
       <div>
          <el-dialog title="请填写信息" :visible.sync="dialogFormVisible" width='40%'>
          <el-form :model="form">
            <el-form-item label="员工姓名" label-width= "15%">
              <el-input v-model="form.ename" autocomplete="off" style="width:90%"></el-input>
            </el-form-item>
            <el-form-item label="账号" label-width= "15%">
              <el-input v-model="form.account" autocomplete="off" style="width:90%"></el-input>
            </el-form-item>
            <el-form-item label="密码" label-width= "15%">
              <el-input v-model="form.password" autocomplete="off" style="width:90%"></el-input>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click="dialogFormVisible = false">取消</el-button>
            <el-button type="primary" @click='submit()'>确定</el-button>
          </div>
          </el-dialog>
        </div>
  </div>
</template>

<script>
import request from '@/utils/request'


  export default {
    data() {
      return {
        params: {
          ename: '',
          pageNum: 1,
          pageSize: 10
        },
        total:0,
        form:{},
        dialogFormVisible: false,
        tableData: []
      }
    },

    // 页面加载时，在created中做一些事情
    created() {
      this.searchUser();
    },
    
    // 定义一些页面上控件触发的方法
    methods: {
      // 通过姓名和手机号码查询内容
      searchUser() {
        request.get("/user/searchUser", {
          params: this.params
        }).then(res => {
          if (res.code === '0') {
            this.tableData = res.data.list;
            this.total = res.data.total;
          } else {
            this.$message({
              message: res.msg,
              type: 'error'
            });
          }
        })
      },

      // 清空输入空内容 --> 查询所有
      reset() {
        this.params = {
          ename: '',
          pageNum: 1,
          pageSize: 10
        }
        this.searchUser();
      },

      // 显示出对话框
      insert() {
        this.form = {};
        this.dialogFormVisible = true;
      },
      
      // 编辑内容，用于更新
      edit(obj) {
        this.form = obj;
        this.dialogFormVisible = true;
      },

      // 提交插入的内容
      submit() {
        request.post("/user/insert", this.form).then(res => {
          if (res.code === '0') {
            this.$message({
              message: '操作成功',
              type: 'success'
            });
            this.dialogFormVisible = false;
            this.searchUser();
          } else {
            this.$message({
              message: res.msg,
              type: 'error'
            });
          }
        })
      },

      // 删除内容
      del(eno) {
        request.delete("/user/delete/" + eno).then(res => {
          if (res.code === '0') {
            this.$message({
              message: '删除成功',
              type: 'success'
            });
            this.searchUser();
            this.tableData = this.tableData.filter(item => item.eno !== eno);
            this.total -= 1;  
          } else {
            this.$message({
              message: res.msg,
              type: 'error'
            });
          }
        })
      },      

      // 每页条数改变触发
      handleSizeChange(pageSize) {
        // 改变每页显示条数
        this.params.pageSize = pageSize; 
        // 再做一次查询
        this.searchUser();
      },

      // 页码改变触发
      handleCurrentChange(currentPage) {
        this.params.pageNum = currentPage;
        this.searchUser();
      },

    }
  }

</script>